//
//  BGLoginViewController.m
//  bigMall
//
//  Created by ios on 16/7/19.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGLoginViewController.h"
#import "BGDateResult.h"
#import "BGProfileViewController.h"
#import "BGFindPwdViewController.h"
#import "BGRegisterViewController.h"
#import "BGUserInfoMode.h"
#import "MJExtension.h"
#import "BGLoginMode.h"
#import "MBProgressHUD.h"
#import "MBProgressHUD+MJ.h"

@interface BGLoginViewController () {
    
    UIButton *_btn;//登陆的按钮
    UITextField *numTextFiled;//账号的
    UITextField *pwdTextFiled;//密码的
    UITextField *authFiled;//验证码
    
}

@end

@implementation BGLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"登陆";
    
    [self createUI];
    
    numTextFiled.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"numTextFiled"];
    pwdTextFiled.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"pwdTextFiled"];
    UIBarButtonItem *backIetm = [[UIBarButtonItem alloc] init];
    backIetm.title =@"";
    self.navigationItem.backBarButtonItem = backIetm;
    
}

- (void)createUI {
    
    for (int i = 1; i <= 3; i++) {
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(21, (i - 1) * 64 + 64, self.view.bounds.size.width - 42, 1)];
        
        if (i == 1) {
            
            view.hidden = YES;
       
        }
        
        view.backgroundColor = [UIColor lightGrayColor];
        
        view.alpha = 0.3;
        
        [self.view addSubview:view];
        
    }
    
    //账号的
    UILabel *numLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 64 / 3 + 64, BGWidth / 6, 64 / 3)];
    
    numLab.text = @"账号：";
    numLab.font = [UIFont systemFontOfSize:15];
    numLab.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:numLab];
    
    numTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(numLab.right, numLab.y, 160, numLab.height)];
    
//    numTextFiled.clearButtonMode = UITextFieldViewModeAlways;
    numTextFiled.placeholder = @"请输入账号";
    
    [self.view addSubview:numTextFiled];
    
    //密码的
    UILabel *pwdLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 256 / 3 + 1 + 64, BGWidth / 6, 64 / 3)];
    
    pwdLab.text = @"密码：";
    pwdLab.font = [UIFont systemFontOfSize:15];
    pwdLab.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:pwdLab];
    
    pwdTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(pwdLab.right, pwdLab.y, 160, pwdLab.height)];
    
    pwdTextFiled.placeholder = @"请输入密码";
    
    pwdTextFiled.secureTextEntry = YES;
    
    [self.view addSubview:pwdTextFiled];
    
//    //验证码
//    UILabel *authLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 64 * 7 / 3 + 2 + 64, BGWidth / 4, 64 / 3)];
//    
//    authLab.text = @"验证码：";
//    authLab.font = [UIFont systemFontOfSize:15];
//    authLab.textAlignment = NSTextAlignmentLeft;
//    [self.view addSubview:authLab];
//    
//    authFiled = [[UITextField alloc] initWithFrame:CGRectMake(authLab.right, authLab.y, BGWidth / 2.5, authLab.height)];
//    
//    authFiled.placeholder = @"请输入验证码";
//    
//    authFiled.secureTextEntry = YES;
//    
//    [self.view addSubview:authFiled];
//    
//    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(authFiled.right, authLab.y - 15, BGWidth - 21 - authFiled.right, authLab.height + 30)];
//    
//    imageView.contentMode = UIViewContentModeScaleAspectFit;
//    
//    imageView.backgroundColor = [UIColor orangeColor];
//    
//    [self.view addSubview:imageView];
//    
    //忘记了密码
    UIButton *forgetBtn = [[UIButton alloc] initWithFrame:CGRectMake(21, 64 * 2 + 10 + 64, BGWidth / 4, 19)];
    
    [forgetBtn setTitle:@"忘记密码" forState:UIControlStateNormal];
    
    [forgetBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

    forgetBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [forgetBtn addTarget:self action:@selector(findPwd) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:forgetBtn];
    
    //注册的账号
    UIButton *resignBtn = [[UIButton alloc] initWithFrame:CGRectMake(BGWidth - 21 - BGWidth / 4, 64 * 2 + 10 + 64, BGWidth / 4, 19)];
    
    [resignBtn setTitle:@"注册账号" forState:UIControlStateNormal];
    
    [resignBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    
    resignBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [resignBtn addTarget:self action:@selector(registerBtn) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:resignBtn];
    
    //login
    _btn = [[UIButton alloc] initWithFrame:CGRectMake(21, resignBtn.bottom + 37, self.view.bounds.size.width - 42, 50)];
    
    _btn.clipsToBounds = YES;
    
    _btn.layer.cornerRadius = 10;
    
    [_btn setTitle:@"登陆" forState:UIControlStateNormal];
    
    [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    _btn.backgroundColor = [UIColor redColor];
    
    [_btn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_btn];
    
}

- (void)registerBtn {
    
    BGRegisterViewController *Register = [[BGRegisterViewController alloc] init];
    
    self.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:Register animated:YES];
    
}

- (void)findPwd {
    
    BGFindPwdViewController *findPwd = [[BGFindPwdViewController alloc] init];
    
    self.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:findPwd animated:YES];
    
}

- (void)login {
    
    [self loadInter];
    
}

- (void)loadInter {
    
    [MBProgressHUD showMessage:@"登录中..."];
    [BGDateResult logininWithUserName:numTextFiled.text andPwd:pwdTextFiled.text success:^(NSObject *result, NSString *status, NSString *msg) {
        
        [MBProgressHUD hideHUD];
        
        //记录了账号
        [[NSUserDefaults standardUserDefaults] setObject:numTextFiled.text forKey:@"numTextFiled"];
        
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        [[NSUserDefaults standardUserDefaults] setObject:pwdTextFiled.text forKey:@"pwdTextFiled"];
        
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        if ([status isEqual:@"1"]) {
            
            [self loadI];
            
            [self.navigationController popViewControllerAnimated:YES];

            [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:State];
            
            [[NSUserDefaults standardUserDefaults] synchronize];
            
        }else {
            
            [self showMessage:msg];
            
        }
        
        
        
        NSLog(@"success!");
//        NSLog(@"msg ＝ %@",msg);
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:@"网络加载失败!"];
        
        [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:State];
        
        [[NSUserDefaults standardUserDefaults] synchronize];
        
    }];
    
}

- (void)loadI {
    
    [BGDateResult getUserInfoSuccess:^(NSString *status, NSString *msg, NSDictionary *res) {
        
        if ([status intValue] == 1) {
            
//            BGUserInfoMode *model = (BGUserInfoMode *)res;
            
            BGProfileViewController *profile = [[BGProfileViewController alloc] init];
            
            profile.iconName = res[@"name"];
            
            profile.imgsrc = res[@"imgsrc"];
            NSLog(@"%@",res[@"imgsrc"]);
            
        }else {
            
            [self showMessage:msg];
            
        }
        
    } failure:^(NSError *error) {
        
    }];
    
}

- (void)showMessage:(NSString *)message {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    
    [alertController addAction:okAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [numTextFiled resignFirstResponder];
    [pwdTextFiled resignFirstResponder];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
